﻿using MediatR;
using SlaughterItem.Common.Domains.Detection;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace SlaughterItem.API.Application.Commands.Week02._IPQC.DelOrUpIPQC_
{
    public class DelOrUpHandler : IRequestHandler<DelOrUpCommand, Result>
    {
        public IBaseRepository<IPQCData> IPQCDataRepository { get; set; }
        public DelOrUpHandler(IBaseRepository<IPQCData> iPQCDataRepository)
        {
            IPQCDataRepository = iPQCDataRepository;
        }
        /// <summary>
        /// 修改+删除
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(DelOrUpCommand request, CancellationToken cancellationToken)
        {
            var result=new Result() { Code=(int)CommonErrorCode.Delete};
            var list = IPQCDataRepository.GetAll().FirstOrDefault(x => x.IPQCId == request.IPQCId);
            if (list != null)
            {
                if (request.Pid == 1)
                {
                    list.IsDeleteIPQC = true;
                    var DelNum = IPQCDataRepository.Update(list);
                    if (DelNum > 0)
                    {
                        result.Code = (int)CommonErrorCode.Success;
                        return result;
                    }
                    else
                    {
                        return result;
                    }
                }
                else if (request.Pid == 2)
                {
                    var UpBoolData = list.IsIPQC == true ? false : true;
                    list.IsIPQC = UpBoolData;
                    var UpNum = IPQCDataRepository.Update(list);
                    if (UpNum > 0)
                    {
                        result.Code = (int)CommonErrorCode.Success;
                        return result;
                    }
                    else
                    {
                        return result;
                    }
                }
                else
                {
                    return result;
                }
            }
            else
            {
                return result;
            }
        }
    }
}
